package component.headerNavigation.homeMenu
{
	import component.headerNavigation.data.MenuData;
	import component.headerNavigation.event.MenuEvent;
	import component.headerNavigation.event.PathEvent;
	import component.headerNavigation.item.HomeMenuItem;
	import component.headerNavigation.sectionMenu.SectionMenu;
	import component.headerNavigation.sectionMenu.SectionMenuClass;
	
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Dictionary;
	import flash.utils.Timer;

	public class HomeMenuClass extends SectionMenuClass
	{
		public var sectionMenuXOffset : Number = 0;
		public var sectionMenuYOffset : Number = 0;
		
		public function set sectionMenuShowDelay(value : Number) : void
		{
			value = (value >= 0) ? value : 0;
			
			if (targetSectionMenuShowDelayTimer.delay != value * 1000)
			{
				targetSectionMenuShowDelayTimer.delay = value * 1000;
			}
		}		
		
		protected var sectionMenues : Dictionary;
		
		protected var targetHomeMenuItem : HomeMenuItem;
		protected var targetSectionMenu : SectionMenu;
		protected var targetSectionMenuShowDelayTimer : Timer;
		
		public function HomeMenuClass()
		{
			super();
			
			sectionMenues = new Dictionary();
			
			targetSectionMenuShowDelayTimer = new Timer(0, 1);
			targetSectionMenuShowDelayTimer.addEventListener(TimerEvent.TIMER_COMPLETE, 
				targetSectionMenuShowDelayTimerComlete);
		}
		
		override public function show() : void
		{
			dispatchEvent(new MenuEvent(MenuEvent.SHOW, false));
		}
		override public function hide() : void
		{
			dispatchEvent(new MenuEvent(MenuEvent.HIDE, false));
		}
		
		override protected function initializeMenuItems() : void
		{	
			menuItems = new Array();
			
			for each (var menuData : MenuData in _itemsData)
			{
				//Initialize home menu items
				var menuItem : HomeMenuItem = new HomeMenuItem();
				menuItem.data = menuData;
				
				menuItem.addEventListener(MouseEvent.CLICK, handleItemClick, false, 0, true);
				menuItem.addEventListener(MouseEvent.ROLL_OVER, handleItemRollOver, false, 0, true);
				menuItem.addEventListener(MouseEvent.ROLL_OUT, handleItemRollOut, false, 0, true);
				
				menuItems.push(menuItem);
				addContentChild(menuItem);
				
				//Initialize section menues
				var menu : SectionMenu = new SectionMenu();
				menu.itemsData = menuData.itemsData;
				
				menu.addEventListener(PathEvent.PATH_CHANGE, handleSectionMenuSelectedPathChange, false, int.MAX_VALUE, true);
				menu.addEventListener(MouseEvent.ROLL_OUT, handleMenuRollOut, false, 0, true);
				menu.addEventListener(MenuEvent.HIDE, handleSectionMenuHide, false, 0, true);
				menu.addEventListener(MenuEvent.SHOW, handleSectionMenuShow, false, 0, true);
				
				sectionMenues[menuData] = menu;			
			}
			
			layout.items = menuItems;
		}
		
		override protected function selectMenuItem(path : String) : void 
		{	
			var menuItem : HomeMenuItem;
			
			for each (menuItem in menuItems)
			{
				if (menuItem.data.id == path)
				{
					menuItem.selected = true;
					return;
				}
			}
			
			for each (menuItem in menuItems)
			{
				var menuItemPathSplit : Array = menuItem.data.id.split(".");
				var pathSplit : Array = path.split(".");
				
				if (menuItemPathSplit.length < pathSplit.length)
				{
					if (menuItemPathSplit.join(".") == pathSplit.slice(0, menuItemPathSplit.length).join(""))
					{
						menuItem.selected = true;
						SectionMenu(sectionMenues[menuItem.data]).selectedPath = path;
						return;
					}
				}
			}
		}
		
		override protected function deselectAllMenuItems() : void
		{
			for each (var menuItem : HomeMenuItem in menuItems)
			{
				menuItem.selected = false;
			}
			
			for each (var sectionMenu : SectionMenu in sectionMenues)
			{
				sectionMenu.selectedPath = null;
			}
		}
		
		protected function handleSectionMenuSelectedPathChange(event : PathEvent) : void
		{	
			event.stopPropagation();
			event.stopImmediatePropagation();
			
			if (event.newPath == "")	
			{
				_selectedPath = "";
				setSelectedPath(event.oldPath, false);
			}
			else
			{
				setSelectedPath(event.newPath, true);
			}
		}
		
		private function handleItemClick(e : MouseEvent) : void
		{
			var menuItem : HomeMenuItem = HomeMenuItem(e.target);
			if (menuItem.enabled)
			{
				if (menuItem.data.id != selectedPath)
				{
					setSelectedPath(menuItem.data.id , true);
				}
				else
				{
					//setSelectedPath("", true);
				}
			}			
		}
		
		
		protected function handleItemRollOver(e : MouseEvent) : void 
		{
			var homeMenuItem : HomeMenuItem = HomeMenuItem(e.target);
			
			if (homeMenuItem != targetHomeMenuItem && homeMenuItem.enabled)
			{
				resetTargets();
				
				targetHomeMenuItem = homeMenuItem;
				targetSectionMenu = sectionMenues[targetHomeMenuItem.data];
				
				targetSectionMenuShowDelayTimer.start();
			}
		}
		
		protected function handleItemRollOut(e : MouseEvent) : void 
		{
			var homeMenuItem : HomeMenuItem = HomeMenuItem(e.target);
			
			if (homeMenuItem == targetHomeMenuItem && 
				!targetSectionMenu.hitTestPoint(stage.mouseX, stage.mouseY, true))
			{
				resetTargets();
			}
		}
		
		protected function handleMenuRollOut(e : MouseEvent) : void 
		{
			var sectionMenu : SectionMenu = SectionMenu(e.target);
			
			if (targetHomeMenuItem != null && 
				!targetHomeMenuItem.hitTestPoint(stage.mouseX, stage.mouseY, true) &&
				sectionMenu == targetSectionMenu)
			{	
				resetTargets();
			}
		}
		
		protected function handleSectionMenuHide(e : MenuEvent) : void 
		{	
			var sectionMenu : SectionMenu = SectionMenu(e.target);
			
			if (contentPane.contains(sectionMenu))
			{
				contentPane.removeChild(sectionMenu);
			}
		}
		
		protected function handleSectionMenuShow(e : MenuEvent) : void
		{
			var sectionMenu : SectionMenu = SectionMenu(e.target);
			
			if (targetSectionMenu == sectionMenu && !contentPane.contains(sectionMenu))
			{
				contentPane.addChild(sectionMenu);
			}
		}
		
		private function resetTargets() : void
		{
			if (targetSectionMenu)
			{
				targetSectionMenu.hide();
			}
			
			targetSectionMenuShowDelayTimer.reset();
			targetHomeMenuItem = null;
			targetSectionMenu = null;
						
		}
		
		private function targetSectionMenuShowDelayTimerComlete(event : TimerEvent) : void
		{	
			if (targetHomeMenuItem && targetSectionMenu)
			{
				targetSectionMenu.x = targetHomeMenuItem.x + sectionMenuXOffset;
				targetSectionMenu.y = targetHomeMenuItem.y + targetHomeMenuItem.height + sectionMenuYOffset;
				
				targetSectionMenu.show();
			}
		}
	}
}